package lip6.ivi;

import java.util.ArrayList;


public class Matrix {
	protected double [][] values;
	protected int numberOfRows;
	protected int numberOfColumns;

	public Matrix(int numberOfRows,int numberOfColumns)
	{
		this.numberOfRows = numberOfRows;
		this.numberOfColumns = numberOfColumns;
		values = new double [numberOfRows][numberOfColumns];
	}
	public void setValue(int r,int c, double  v)
	{
		values[r][c] = v;
	}

	public double getValue(int r,int c)
	{
		return(values[r][c]);
	}
	
	public int getNumberOfRows(){
		return(numberOfRows);
	}
	
	public int getNumberOfColumns(){
		return(numberOfColumns);
	}

	public double getMaxElement(){
		double result=Double.NEGATIVE_INFINITY;
		for (int i = 0; i < numberOfRows; i ++) {
			for (int j = 0; j < numberOfRows; j ++) {
				if (result < this.getValue(i, j)) {
					result = this.getValue(i, j);
				}
			}
		}
		return result;
	}

	public Vector getRow(int r){
		Vector v = new Vector(this.numberOfColumns);
		for (int i = 0; i < this.numberOfColumns; i++) {
			v.add(i,this.values[r][i]);
		}
		return v;
	}
	public void updateValueOldStates(Matrix m2){
		int rows = this.getNumberOfRows();
		int columns = this.getNumberOfColumns();
		for (int i = 0; i < rows; i ++) {
			for (int j = 0; j < columns; j ++){
			 this.setValue(i, j, m2.getValue(i,j));
			}
		}
	}

	public int getItemMaxRow(){
		int item  = -2;
		boolean resultcompare = false;
		int count = 0;  
		boolean test=false;
		for (int i = 0; i < this.numberOfRows; i ++) {
			Vector v  = new Vector (this.numberOfColumns);
			v=this.getRow(i);  
			for (int j = 0; j < this.numberOfRows; j ++){
				Vector v2 = new Vector (this.numberOfColumns);
				v2 = this.getRow(j);
				resultcompare = v.compare(v2);
				if(resultcompare == true) count++;
			}
			if (count == this.numberOfRows) { 
				item = i; 
				test=true;
			}
			count = 0;
		}
		if (test == false) { 
			System.out.println("pas de ligne max");
		} 
		return item;
	}

	public int getSize(){
		return(this.numberOfRows);
	}

	/* Entree: une matrice 
	 * Sortie: un vecteur  trie en ordre croissant.
	 */
	public Vector sortElements(){
	    double tmp1 = 0.0;
		ArrayList<Double> v1 = new ArrayList<Double>();
		int count = 0;
		for (int w = 0; w < this.numberOfRows; w ++) {
			for (int z = 0; z < this.numberOfColumns; z ++) {
				boolean existe = false;
				if (this.getValue(w, z) != tmp1) {
					tmp1 = this.getValue(w,z); 
					for (int t = 0; t < count; t ++) {
						if (v1.get(t) == tmp1) existe = true;
					}
					if (existe== false){
						v1.add(count, tmp1);
					    count++;
					}
				}
			}
		}
		Vector v2 = new Vector(v1.size());
		for (int i = 0; i< v1.size(); i ++){
			v2.add(i, v1.get(i));
		}	
		v2.sort();
		for (int i = 0; i < v2.size(); i ++) {
			System.out.println(" "+v2.get(i));
		}	
		return v2;
	}
	
	public void updateMatrix(Vector v, int i){
		for (int j = 0; j < v.size(); j ++) {
			this.setValue(i, j, v.get(j));
		}
	}
	
	public Vector computeNorme(Matrix m){
		int rows = this.getNumberOfRows();
		Vector v = new Vector(rows);
		int columns = this.getNumberOfColumns();
		for (int i = 0; i < rows; i ++){
			double sum = 0.0;
			for (int j = 0; j < columns; j ++) {
				sum += Math.pow(this.getValue(i,j) - m.getValue(i,j),2);
			}
			v.add(i, Math.sqrt(sum));
		}
		return v;
	}
    	
}
